package org.xxpay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.StatisticsMch;
import org.xxpay.core.entity.StatisticsMchExample;
import org.xxpay.core.service.IStatisticsMchService;
import org.xxpay.service.dao.mapper.StatisticsMchMapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户统计表 服务实现类
 * </p>
 *
 * @author zhuxiao
 * @since 2021-02-26
 */
@Service
public class StatisticsMchServiceImpl implements IStatisticsMchService {

    @Autowired
    private StatisticsMchMapper statisticsMchMapper;

    @Override
    public int add(StatisticsMch statisticsMch) {
        return statisticsMchMapper.insertSelective(statisticsMch);
    }

    @Override
    public int updateByPrimaryKey(StatisticsMch statisticsMch) {
        return statisticsMchMapper.updateByPrimaryKeySelective(statisticsMch);
    }

    @Override
    public StatisticsMch findByPrimaryKey(Long mchId, Date statisticsDate) {
        return statisticsMchMapper.selectByPrimaryKey(mchId, statisticsDate);
    }

    @Override
    public Long count(Date createTimeStart, Date createTimeEnd, Long mchId) {
        Map param = new HashMap<>();
        if(mchId != null) param.put("mchId", mchId);
        if(createTimeStart != null) param.put("createTimeStart", createTimeStart);
        if(createTimeEnd != null) param.put("createTimeEnd", createTimeEnd);
        return statisticsMchMapper.count(param);
    }

    @Override
    public List<StatisticsMch> list(Date createTimeStart, Date createTimeEnd, Long mchId) {
        StatisticsMchExample example = new StatisticsMchExample();
        example.setOrderByClause("statisticsDate desc");
        StatisticsMchExample.Criteria criteria = example.createCriteria();
        if (mchId != null) criteria.andMchIdEqualTo(mchId);
        setCriteria(criteria, null, createTimeStart, createTimeEnd);
        return statisticsMchMapper.selectByExample(example);
    }

    @Override
    public List<Map> list(int pageIndex, int pageSize, Date createTimeStart, Date createTimeEnd, Long mchId) {
        Map param = new HashMap<>();
        param.put("offset", pageIndex);
        param.put("limit", pageSize);
        if(mchId != null) param.put("mchId", mchId);
        if(createTimeStart != null) param.put("createTimeStart", createTimeStart);
        if(createTimeEnd != null) param.put("createTimeEnd", createTimeEnd);
        return statisticsMchMapper.list(param);
    }

    @Override
    public Map countAll(Long mchId) {
        Map param = new HashMap<>();
        if(mchId != null) param.put("mchId", mchId);
        return statisticsMchMapper.countAll(param);
    }

    @Override
    public int updateStatistics(StatisticsMch updateRecord) {
        return statisticsMchMapper.updateStatistics(updateRecord);
    }

    @Override
    public Long count2MchTop(Long agentId, Long mchId, Date createTimeStart, Date createTimeEnd) {
        Map param = new HashMap<>();
        if(mchId != null) param.put("mchId", mchId);
        if(agentId != null) param.put("agentId", agentId);
        if(createTimeStart != null) param.put("createTimeStart", createTimeStart);
        if(createTimeEnd != null) param.put("createTimeEnd", createTimeEnd);
        return statisticsMchMapper.count2MchTop(param);
    }

    @Override
    public List<Map> mchTopList(int pageIndex, int pageSize, Long agentId, Long mchId, Date createTimeStart, Date createTimeEnd) {
        Map param = new HashMap<>();
        param.put("offset", pageIndex);
        param.put("limit", pageSize);
        if(mchId != null) param.put("mchId", mchId);
        if(agentId != null) param.put("agentId", agentId);
        if(createTimeStart != null) param.put("createTimeStart", createTimeStart);
        if(createTimeEnd != null) param.put("createTimeEnd", createTimeEnd);
        return statisticsMchMapper.mchTopList(param);
    }

    void setCriteria(StatisticsMchExample.Criteria criteria, StatisticsMch statisticsMch, Date createTimeStart, Date createTimeEnd) {
        if(statisticsMch != null) {
            if(statisticsMch.getMchId() != null) criteria.andMchIdEqualTo(statisticsMch.getMchId());
        }
        if(createTimeStart != null || createTimeEnd != null) {
            criteria.andStatisticsDateGreaterThanOrEqualTo(createTimeStart);
            criteria.andStatisticsDateLessThanOrEqualTo(createTimeEnd);
        }
    }
}
